package webCrawler;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;

public class LinkFinder implements Runnable {
	
	//
	private final String url;
	//
	private final LinkHandler linkHandler;
	//
	private static final long startTime = System.nanoTime();

	/**
	 * @param url
	 * @param linkHandler
	 */
	public LinkFinder(final String url, final LinkHandler linkHandler) {
		this.url = url;
		this.linkHandler = linkHandler;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		getSimpleLink(url);
	}

	private void getSimpleLink(final String url) {
		// TODO Auto-generated method stub
		if (!linkHandler.isVisited(url)) {
			try {
				URL urlLink = new URL(url);
				Parser parser = new Parser(urlLink.openConnection());
				NodeList list = parser.extractAllNodesThatMatch(new NodeClassFilter(LinkTag.class));
				List<String> urls = new ArrayList<>();
				
				for (int i = 0; i < list.size(); i++) {
					LinkTag extracted = (LinkTag)list.elementAt(i);
					
					if (!extracted.getLink().isEmpty() && !linkHandler.isVisited(extracted.getLink())) {
						urls.add(extracted.getLink());
					}
				}
				linkHandler.addVisitedLink(url);
//				System.out.println("Done with " + url);
				
				if (linkHandler.getNumberOfVisitedLinks() == 150) {
					System.out.println("Time to visit 150 distinct links = " + (System.nanoTime() - startTime)/1.0e9);
				}
				
				for (String l : urls) {
//					System.out.println("About to process " + l);
					linkHandler.queueLink(l);
				}
			} catch (Exception e) {
				
			}
		}
	}

}
